<?php

require 'vendor/autoload.php';

use Symfony\Component\DomCrawler\Crawler;

$t = 4790;
$host = 'http://www.it-ebooks.info';

$client = new GuzzleHttp\Client([
    'defaults' => [
        'allow_redirects' => false
    ]
]);
$crawler = new Crawler();

$f = fopen("books/total.json", "w");
for ($i = 1; $i <= $t; $i++) {
    $url = $host . '/book/' . $i . '/';
    echo 'GET ', $url, PHP_EOL;
    try {
        $response = $client->get($url);
        $statusCode = $response->getStatusCode();
        echo $statusCode, ' ', $url, PHP_EOL;
        if ($statusCode == '200') {
            $crawler->clear();
            $crawler->addContent($response->getBody());
            $book = new stdClass();

            $h1 = $crawler->filterXPath('//h1');
            $book->title = $h1->text();
            $book->h3 = $h1->siblings()->first()->text();

            $book->img = $host . $crawler->filterXPath('//img[@class="img"]')->attr('src');

            $book->desc = $crawler->filterXPath('//span[@itemprop="description"]')->text();

            $book->author = $crawler->filterXPath('//b[@itemprop="author"]')->text();
            $book->isbn = $crawler->filterXPath('//b[@itemprop="isbn"]')->text();
            $book->year = $crawler->filterXPath('//b[@itemprop="datePublished"]')->text();
            $anchors = $crawler->filterXPath('//table[last()-1]//td[contains(text(),"Download:")]');
            $book->pdf = $anchors->first()->siblings()->first()->filterXPath('//a')->attr('href');

            fwrite($f, json_encode($book) . "\n");

            if ($i % 100 == 0) {
                fflush($f);
            }
        } else {
            var_dump($response);
        }
    } catch (GuzzleHttp\Exception\AdapterException $e) {
        echo $e->getMessage(), $i;
    }
}
fclose($f);
